#include "chartwidget.h"


ChartWidget::ChartWidget(QWidget *parent) : QWidget(parent)
{
    tagLabel = new QLabel("销售统计");
    tagLabel->setFixedHeight(20);
    tagLabel->setFrameStyle(QLabel::StyledPanel);
    tagLabel->setAlignment(Qt::AlignCenter);
    factoryComboBox = new QComboBox;
    updateButton = new QPushButton("更新显示");
    QSqlQueryModel* factoryModel = new QSqlQueryModel(this);
    factoryModel->setQuery("select name from factory");    //得到数据
    factoryComboBox->setModel(factoryModel);    //设置到Box

    chartModel = new QStandardItemModel(this);
    chartModel->setColumnCount(2);
    chartModel->setHeaderData(0, Qt::Horizontal, QString("品牌"));
    chartModel->setHeaderData(1, Qt::Horizontal, QString("销售数量"));
    QTableView* table = new QTableView;
    PieView* pieChart = new PieView;
    table->setModel(chartModel);
    pieChart->setModel(chartModel);
    QItemSelectionModel* selectionModel = new QItemSelectionModel(chartModel);
    table->setSelectionModel(selectionModel);
    pieChart->setSelectionModel(selectionModel);

    QVBoxLayout* topLayout = new QVBoxLayout;
    topLayout->addSpacing(20);
    topLayout->addWidget(tagLabel);
    QGridLayout* mainLayout = new QGridLayout(this);
    mainLayout->addLayout(topLayout, 0, 0, 1, 4);
    mainLayout->addWidget(factoryComboBox, 1, 0);
    mainLayout->addWidget(updateButton, 1, 1);
    mainLayout->addWidget(table, 2, 0, 10, 2);
    mainLayout->addWidget(pieChart, 2, 2, 10, 2);

    connect(updateButton, SIGNAL(clicked(bool)), this, SLOT(slotUpdateChart()));
    connect(factoryComboBox, SIGNAL(currentIndexChanged(QString)), this, SLOT(slotUpdateChart()));
}

void ChartWidget::slotUpdateChart()
{
    if(factoryComboBox->currentText() != "请选择厂家")
    {
        showChart();
    }
}

void ChartWidget::showChart()
{
    QSqlQuery query;
    query.exec(QString("select name,sell from brand where factory='%1'").arg(factoryComboBox->currentText()));
    chartModel->removeRows(0, chartModel->rowCount(QModelIndex()), QModelIndex());
    int row = 0;
    while(query.next())
    {
        int r = qrand() % 256;
        int g = qrand() % 256;
        int b = qrand() % 256;
        chartModel->insertRows(row, 1, QModelIndex());
        chartModel->setData(chartModel->index(row, 0, QModelIndex()), query.value(0).toString());
        chartModel->setData(chartModel->index(row, 1, QModelIndex()), query.value(1).toInt());
        chartModel->setData(chartModel->index(row, 0, QModelIndex()), QColor(r, g, b), Qt::DecorationRole);
        row++;
    }
}


PieView::PieView(QWidget* parent) :
    QAbstractItemView(parent)
{}
void PieView::paintEvent(QPaintEvent*)
{
    QPainter painter(viewport());
    painter.setPen(Qt::black);
    int x = 40;
    int y = 250;
    painter.drawLine(x, y, 40, 30);
    painter.drawLine(38, 32, 40, 30);
    painter.drawText(20, 30, "销售数量");
    for(int i = 1; i < 5; i++)
    {
        painter.drawLine(-1, - i * 50, 1, - i * 50);
        painter.drawText(-20, - i * 50, tr("%1").arg(i * 5));
    }

    painter.drawLine(x, y, 540, 250);
    painter.drawLine(538, 248, 540, 250);
    painter.drawLine(540, 250, 538, 252);
    painter.drawText(545, 250, "品牌");
    int pos = x + 20;
    int row;
    for(row = 0; row < model()->rowCount(rootIndex()); row++)
    {
        QModelIndex index = model()->index(row, 0, rootIndex());
        QString dep = model()->data(index).toString();
        painter.drawText(pos, y + 20, dep);
        pos += 50;
    }
    int posN = x + 20;
    for(row = 0; row < model()->rowCount(rootIndex()); row++)
    {
        QModelIndex index = model()->index(row, 1, rootIndex());
        int sell = model()->data(index).toDouble();
        int width = 10;
        QModelIndex colorIndex = model()->index(row, 0, rootIndex());
        QColor color = QColor(model()->data(colorIndex, Qt::DecorationRole).toString());
        if(selections->isSelected(index))
        {
            painter.setBrush(QBrush(color, Qt::Dense3Pattern));
        }
        else
        {
            painter.setBrush(QBrush(color));
        }
        painter.drawRect(QRect(posN, y - sell * 10, width, sell * 10));
        QRegion regionM(posN, y - sell * 10, width, sell * 10);
        regionList << regionM;
        posN += 50;
    }

}
void PieView::setSelectionModel(QItemSelectionModel* selectionMode)
{
    selections = selectionMode;
}
QRegion PieView::itemRegion(QModelIndex index)
{
    QRegion region;
    if(index.column() == 1)
    {
        region = regionList[index.row()];
    }
    return region;
}
QModelIndex PieView::indexAt(const QPoint &point) const
{
    QPoint newPoint(point.x(), point.y());
    QRegion region;
    foreach (region, regionList)
    {
        if(region.contains(newPoint))
        {
            int row = regionList.indexOf(region);
            QModelIndex index = model()->index(row, 1, rootIndex());
            return index;
        }
    }
    return QModelIndex();
}
//以下函数未实现,但必须声明

QRect PieView::visualRect(const QModelIndex &index) const{}
void PieView::scrollTo(const QModelIndex &index, ScrollHint hint){}
QModelIndex PieView::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers){}
int PieView::horizontalOffset() const{}
int PieView::verticalOffset() const{}
bool PieView::isIndexHidden(const QModelIndex &index) const{}
void PieView::setSelection(const QRect &rect, QItemSelectionModel::SelectionFlags command){}
QRegion PieView::visualRegionForSelection(const QItemSelection &selection) const{}
